﻿using Himp.TaskScheduling;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Himp.TaskScheduling
{
    /// <summary>
    /// 实例服务
    /// </summary>
    public class WorkflowInstanceService : TaskSchedulingAppService, IWorkflowInstanceService
    {
        private ActiviWorkflowManager _workflowManager;
        private IRepository<BizflowConfig, Guid> _bizflowConfigRepository;

        public WorkflowInstanceService(ActiviWorkflowManager workflowManager,
            IRepository<BizflowConfig, Guid> bizflowConfigRepository)
        {
            _workflowManager = workflowManager;
            _bizflowConfigRepository = bizflowConfigRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<WorkflowInstanceDto>> GetListAsync(WorkflowInstanceInput input)
        {
            string begntime = "";
            string endtime = "";

            if (!input.Begntime.HasValue)
            {
                begntime = DateTime.Now.Date.ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                begntime = input.Begntime.Value.ToString("yyyy-MM-dd HH:mm:ss");
            }

            if (!input.Endtime.HasValue)
            {
                endtime = DateTime.Now.Date.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                endtime = input.Endtime.Value.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss");
            }

            var list = await _workflowManager.GetProcessInstance(begntime, endtime, input.Status);
            var itemsDto = ObjectMapper.Map<List<WorkflowInstance>, List<WorkflowInstanceDto>>(list);

            if (input.Pi == 0)
            {
                return new PagedResultDto<WorkflowInstanceDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = itemsDto.Count();
                var pagedItems = itemsDto.Skip((input.Pi - 1) * input.Ps).Take(input.Ps);

                return new PagedResultDto<WorkflowInstanceDto>(totalCount, pagedItems.ToList());
            }
        }

        /// <summary>
        /// 启动流程
        /// </summary>
        /// <returns></returns>
        public async Task<WorkflowInstanceStasDto> PostAsync(WorkflowInstanceCreateDto input)
        {
            string userId = string.IsNullOrEmpty(input.UserId) ? "admin" : input.UserId;
            string bizFlowCode = input.BizFlowCode; //业务流程代码
            var bizflowConfigList = await _bizflowConfigRepository.GetListAsync(w => w.BizflowCode == bizFlowCode);
            var bizflow = bizflowConfigList.FirstOrDefault();
            string workflowModelKey = bizflow.WorkflowModelKey;
            var bizPars = input.BizPars;
            var res = await _workflowManager.StartWorkflowInstance(workflowModelKey, input.AssocUsers, userId, bizPars);
            var itemDto = ObjectMapper.Map<WorkflowInstanceStas, WorkflowInstanceStasDto>(res);

            return itemDto;
        }

        /// <summary>
        /// 执行流程
        /// </summary>
        /// <returns></returns>
        public async Task<WorkflowInstanceStasDto> PutAsync(WorkflowInstanceExecDto input)
        {
            string processInstanceId = input.ProcessInstanceId;
            string userId = string.IsNullOrEmpty(input.UserId) ? "admin" : input.UserId;
            var res = await _workflowManager.ExecWorkflowInstance(processInstanceId, userId, input.BizPars);
            var itemDto = ObjectMapper.Map<WorkflowInstanceStas, WorkflowInstanceStasDto>(res);

            return itemDto;
        }
    }
}
